//
//  AppDelegate.m
//  DemoProject
//
//  Created by liu xiwang on 2018/4/10.
//  Copyright © 2018年 Sunqk. All rights reserved.
//

#import "SunAppDelegate.h"
#import "TabBarControllerConfig.h"
#import "CYLPlusButtonSubclass.h"
#import "UIView+AnimationProperty.h"

#import "NewFeatureVC.h"
#import "SDWebImageManager.h"
#import "WRNavigationBar.h"
#import "BHTimeProfiler.h"
#import "AppConfigServiceProtocol.h"
@interface SunAppDelegate ()<UITabBarControllerDelegate, CYLTabBarControllerDelegate>

@end

@implementation SunAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"---------");
    NSLog(@"---------");
    
    [BHContext shareInstance].application = application;
    [BHContext shareInstance].launchOptions = launchOptions;
    [BHContext shareInstance].moduleConfigName = @"BeeHive.bundle/BeeHive";//可选，默认为BeeHive.bundle/BeeHive.plist
    [BHContext shareInstance].serviceConfigName = @"BeeHive.bundle/BHService";
    [BeeHive shareInstance].enableException = YES;
    [[BeeHive shareInstance] setContext:[BHContext shareInstance]];
    [[BHTimeProfiler sharedTimeProfiler] recordEventTime:@"BeeHive::super start launch"];
    [super application:application didFinishLaunchingWithOptions:launchOptions];
    //第三方及App配置的初始化
    id<AppConfigServiceProtocol> appConfig = [[BeeHive shareInstance] createService:@protocol(AppConfigServiceProtocol)];
    if ([appConfig isKindOfClass:[NSObject class]]) {
        [appConfig registerAllThirdParty:launchOptions];
    }
    NSLog(@"---------");

    
    self.window = [[UIWindow alloc]init];
    self.window.frame = [UIScreen mainScreen].bounds;
    self.window.rootViewController = [self createRootViewController];
    [self setNavBarAppearence];
    [self.window makeKeyAndVisible];
    
    
    return YES;
}

- (UIViewController *)createRootViewController {
    if (![CurrentAppVersion isEqualToString:[UserDefaults objectForKey:@"version"]]) { // 如果是第一次进入新版本,进入介绍页面
        [UserDefaults setObject:CurrentAppVersion forKey:@"version"];//保存现在的版本
        [UserDefaults synchronize];
        NewFeatureVC * vcNew = [[NewFeatureVC alloc] init];
        __unsafe_unretained SunAppDelegate *app = self;
        vcNew.start = ^{
            TabBarControllerConfig *tabBarControllerConfig = [[TabBarControllerConfig alloc] init];
            CYLTabBarController *tabBarController = tabBarControllerConfig.tabBarController;
            tabBarController.delegate = self;
            app.window.rootViewController = tabBarController;
        };
        return vcNew;
    } else { // 否则，直接进入TabbarController
        TabBarControllerConfig *tabBarControllerConfig = [[TabBarControllerConfig alloc] init];
        CYLTabBarController *tabBarController = tabBarControllerConfig.tabBarController;
        tabBarController.delegate = self;
        return tabBarController;
    }
}


- (void)setNavBarAppearence{
    // 设置导航栏默认的背景颜色
    [WRNavigationBar wr_setDefaultNavBarBarTintColor:[UIColor colorWithRed:0/255.0 green:175/255.0 blue:240/255.0 alpha:1]];
    // 设置导航栏所有按钮的默认颜色
    [WRNavigationBar wr_setDefaultNavBarTintColor:[UIColor whiteColor]];
    // 设置导航栏标题默认颜色
    [WRNavigationBar wr_setDefaultNavBarTitleColor:[UIColor whiteColor]];
    // 统一设置状态栏样式
    [WRNavigationBar wr_setDefaultStatusBarStyle:UIStatusBarStyleLightContent];
    // 如果需要设置导航栏底部分割线隐藏，可以在这里统一设置
    [WRNavigationBar wr_setDefaultNavBarShadowImageHidden:YES];
}

#pragma mark - tabbarDelegate

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    [[self cyl_tabBarController] updateSelectionStatusIfNeededForTabBarController:tabBarController shouldSelectViewController:viewController];
    return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectControl:(UIControl *)control {
 
}

#pragma mark - 设置启动页
- (void)setLanuchImage {
//    UIImageView *iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
//    iconImageView.image        = [[ApplicationUtil sharedApplicationUtil] launchImage];
//    [self.window addSubview:iconImageView];
//    [UIView animateKeyframesWithDuration:1.f delay:0.f options:0 animations:^{
//        iconImageView.scale = 1.2f;
//        iconImageView.alpha = 0.f;
//    } completion:^(BOOL finished) {
//        [iconImageView removeFromSuperview];
//    }];
}

//内存处理
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application{
    [[SDWebImageManager sharedManager].imageCache clearMemory];
    [[SDWebImageManager sharedManager]cancelAll];
}

- (void)applicationWillResignActive:(UIApplication *)application {

}


- (void)applicationDidEnterBackground:(UIApplication *)application {

}


- (void)applicationWillEnterForeground:(UIApplication *)application {

}


- (void)applicationDidBecomeActive:(UIApplication *)application {

}


- (void)applicationWillTerminate:(UIApplication *)application {

}


@end
